package br.com.simon.dao;

import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Restrictions;

import br.com.caelum.vraptor.ioc.Component;
import br.com.simon.modelo.Turma;

@Component
public class TurmaDao {

	private Session session;

	public TurmaDao(Session session) {
		this.session = session;
	}

	public void salva(Turma turma) {
		Transaction transaction = session.beginTransaction();
		session.save(turma);
		transaction.commit();
	}

	@SuppressWarnings("unchecked")
	public List<Turma> listaTudo() {
		Criteria criteria = session.createCriteria(Turma.class);
		return criteria.list();
	}

	public Turma getTurma(Long id) {
		return (Turma) session.load(Turma.class, id);
	}

	public void atualiza(Turma turma) {
		Transaction transaction = session.beginTransaction();
		session.update(turma);
		transaction.commit();
	}

	public void remove(Turma turma) {
		Transaction transaction = session.beginTransaction();
		session.delete(turma);
		transaction.commit();
	}

	@SuppressWarnings("unchecked")
	public List<Turma> busca(String id) {
		Criteria criteria = session.createCriteria(Turma.class);
		criteria.add(Restrictions.ilike("id", id, MatchMode.ANYWHERE));
		return criteria.list();
	}

	public void recarrega(Turma turma) {
		session.refresh(turma);
	}
	
}
